import { useUserStore } from "@/stores/user";
import { watch } from "vue";
const env = import.meta.env.VITE_APP_TITLE
const systemDataStore = useUserStore();
export default {
  mounted(el, binding) {
    if (env === 'dev' || env === 'test') {
      el.style.display = '';
      return;
    }
    const permission = binding.value; // 传入的权限标识
    // 检查权限的功能
    const checkPermission = () => {
      const permissionList = systemDataStore.menuBtnData || []; // 菜单中的按钮权限数据
      // 检查权限
      if (!permissionList.includes(permission)) {
        el.style.display = 'none';
      } else {
        el.style.display = ''; // 如果有权限，确保元素显示
      }
    };
    // 观察 `menuBtnData` 的变化
    const stopWatch = watch(
      () => systemDataStore.menuBtnData,
      (newValue) => {
        checkPermission();
      },
      { immediate: true }
    );
    // 清理观察
    el._onUnmount = () => {
      stopWatch();
    };
    checkPermission();
  },
  unmounted(el) {
    // 清理逻辑
    if (el._onUnmount) {
      el._onUnmount(); // 停止监听
    }
  }
};